package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.SetmealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
@Api(tags = "套餐相关接口")
public class SetmealController {
    @Autowired(required = false)
    private SetmealService setmealService;
    @CacheEvict(value = "setmealCache",allEntries = true)
    @PostMapping
    @ApiOperation(value = "新增套餐接口")
    public R<String> saveWithDish(@RequestBody SetmealDto setmealDto, HttpSession session){
        Long empId = (Long) session.getAttribute("employee");
        setmealDto.setCreateUser(empId);
        setmealDto.setUpdateUser(empId);
        setmealService.saveWithDish(setmealDto);
        return R.success("添加成功！");
    }
    @GetMapping("/page")
    @ApiOperation(value = "套餐分页接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码",required = true),
            @ApiImplicitParam(name = "pageSize",value = "页面大小",required = true),
            @ApiImplicitParam(name = "name",value = "套餐名称",required = false)
    })
    public R<Page<SetmealDto>> page(int page ,int pageSize,String name){
        Page<SetmealDto> setmealDtoPage=setmealService.page(page,pageSize,name);
        return R.success(setmealDtoPage);
    }
    @CacheEvict(value = "setmealCache",allEntries = true)
    @DeleteMapping
    @ApiOperation(value = "套餐删除接口")
    public R<String> delete(@RequestParam List<Long> ids){
        setmealService.delete(ids);
        return R.success("删除成功！");
    }
    //通过id来查询
    @GetMapping("/{id}")
    @ApiOperation(value = "套餐id查询接口")
    public R<SetmealDto> findById(@PathVariable Long id){
        SetmealDto setmealDto=setmealService.findById(id);
        return R.success(setmealDto);
    }
    //修改套餐
    @CacheEvict(value = "setmealCache",allEntries = true)
    @PutMapping
    @ApiOperation(value = "套餐更新接口")
    public R<String> update(@RequestBody SetmealDto setmealDto,HttpSession session){
        Long empId = (Long) session.getAttribute("employee");
        setmealDto.setUpdateUser(empId);
        setmealService.update(setmealDto);
        return R.success("修改成功!");

    }
    //批量停售
    @PostMapping("/status/0")
    @ApiOperation(value = "套餐批量停售接口")
    public R<String> updataStatusZero(@RequestParam List<Long> ids){
        setmealService.updateStatusZero(ids);
        return R.success("批量停售成功！");
    }
    //批量启售
    @PostMapping("/status/1")
    @ApiOperation(value = "套餐批量启售接口")
    public R<String> updataStatusOne(@RequestParam List<Long> ids){
        setmealService.updateStatusOne(ids);
        return R.success("批量启售成功！");
    }
     @GetMapping("/list")
     @ApiOperation(value = "套餐条件查询接口")
    public R<List<SetmealDto>> list(Long categoryId , Integer status){
        List<SetmealDto> setmealDtoList=setmealService.list(categoryId,status);
        return R.success(setmealDtoList);
     }

}
